package com.campus.controller;


import com.campus.entity.Library.LibraryUsage;
import com.campus.entity.ResultBody;
import com.campus.service.LibrarySeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @author LuTanhang
 */
@RestController
@RequestMapping("/library")
@ConditionalOnClass({Date.class})
public class LibraryController {


    @Autowired
    private LibrarySeatService librarySeatService;


    /**
     *
     * @param accountId
     * @param floor
     * @param rows
     * @param column
     * @return 检查当前用户是否具有合法的权限
     */
    @RequestMapping("/check/{floor}/{rows}/{column}")
    public ResultBody checkSeat(@RequestParam("account") String accountId, @PathVariable("floor") Integer floor
    ,@PathVariable("rows") Integer rows,@PathVariable("column") Integer column){
        return librarySeatService.checkSeat(accountId,floor,rows,column);
    }


    //添加一个座位
    @RequestMapping("/add/{floor}/{rows}/{column}")
    public ResultBody addSeat(@RequestParam("account") String accountId, @PathVariable("floor") Integer floor
            , @PathVariable("rows") Integer rows, @PathVariable("column") Integer column, @RequestParam("begin") String beginTime, @RequestParam("end") String endTime) throws ParseException {
        LibraryUsage usage = new LibraryUsage(accountId,floor, rows, column);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        usage.setBeginUsage(new Timestamp(sf.parse(beginTime).getTime()));
        usage.setEndUsage(new Timestamp(sf.parse(endTime).getTime()));

        return librarySeatService.addSeat(usage);
    }

    @RequestMapping("/info/{floor}")
    public ResultBody infoFloor(@PathVariable("floor")Integer floor){
        return librarySeatService.allSeat(floor);
    }
}
